﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_zreport : System.Web.UI.UserControl
{


    protected void setFleetrevenue()
    {
        string res = "";
        int fleetID;
        int TaxiID;
        int ds1rc = 0;
        int ds2rc = 0;
        double taxiDaily = 0;
        double fleetTotal = 0;
        string qryTaxi;

        string qryFlt = "SELECT * FROM [Fleet]"; // SELECT ALL FLEETS
        DataSet ds1 = DBClass.Default.SelectCommand(qryFlt);
        DataSet ds2;
        if (ds1 != null)
        {
            ds1rc = ds1.Tables[0].Rows.Count;
            for (int i = 0; i < ds1rc; i++)
            {
                fleetID = (int)ds1.Tables[0].Rows[i]["ID"];
                fleetTotal = (int)ds1.Tables[0].Rows[i]["TotalRevenue"];
                res += "Fleet " + fleetID.ToString() + " before : " + fleetTotal.ToString() + "<br />";
                // SELECT TAXIS OF EACH FLEET < fleetID
                qryTaxi = "SELECT dbo.Taxi.Revenue, dbo.Taxi.DailyKm, dbo.Taxi.ID FROM dbo.ConnectionTaxiFleet INNER JOIN dbo.Taxi ON dbo.ConnectionTaxiFleet.TaxiID = dbo.Taxi.ID WHERE (dbo.ConnectionTaxİFleet.FleetID = " + fleetID.ToString() + ")";
                ds2 = DBClass.Default.SelectCommand(qryTaxi);
                ds2rc = ds2.Tables[0].Rows.Count;
                taxiDaily = 0;
                for (int j = 0; j < ds2rc; j++)
                {
                    TaxiID = (int)ds2.Tables[0].Rows[j]["ID"];
                    res += "Taxi" + TaxiID + " : " + ds2.Tables[0].Rows[j]["Revenue"].ToString() + "<br />";
                    taxiDaily += (double)ds2.Tables[0].Rows[j]["Revenue"];
                    qryTaxi = "UPDATE dbo.Taxi SET DailyKm =0, Revenue =0 WHERE dbo.Taxi.ID=" + TaxiID;

                    if (DBClass.Default.UpdateCommand(qryTaxi))
                    {
                        DBClass.Default.addLog(Profile.UserName, "taxi-update", qryTaxi, System.DateTime.Now);
                    }
                }
                fleetTotal += taxiDaily;
                qryFlt = "UPDATE dbo.Fleet SET TotalRevenue =" + fleetTotal.ToString() + "WHERE dbo.Fleet.ID=" + fleetID.ToString();
                if (DBClass.Default.UpdateCommand(qryFlt))
                {
                    DBClass.Default.addLog(Profile.UserName, "fleet-update", qryFlt, System.DateTime.Now);
                }
            }

        }
        DBClass.Default.addLog(Profile.UserName, "z-report", res.Substring(0,50) + "...", System.DateTime.Now);
        //this.Label1.Text = res

        Response.Redirect("report.aspx");
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataBind();
        GridView2.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        setFleetrevenue();        
    }
}